import { addOrEditBuildingApi, deleteBuildingApi, fetchBuildingDetailAPI, fetchBuildingListApi } from '@/api/park/buildingApi';

const actions = {
  //用于发送请求获取楼宇列表的action
  async fetchBuildingListAction(context, params) {
    const { data } = await fetchBuildingListApi(params);
    context.commit('fetchBuildingListMutation', data);
  },
  // 用于发送请求删除楼宇的action
  async deleteBuildingAction(context, id) {
    await deleteBuildingApi(id);
    return '删除楼宇成功';
  },
  //发送请求添加楼宇或者编辑楼宇的action
  async addOrEditBuildingAction(context, data) {
    await addOrEditBuildingApi(data);
    return `${data.id ? '编辑楼宇成功' : '添加楼宇成功'}`;
  },
  //发送请求查询楼宇的action
  async fetchBuildingDetailAction(context, id) {
    const { data } = await fetchBuildingDetailAPI(id);
    return data;
  },
};


const mutations = {
  //获取楼宇列表的mutation
  fetchBuildingListMutation(state, { total, rows }) {
    state.total = total;
    state.buildingList = rows;
  },
};


const state = {
  total: 0,
  buildingList: [],
};


export default {
  namespaced: true,
  state,
  mutations,
  actions,
};

